package com.ruoyi.web.controller.manager;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manager.domain.MgServiceManager;
import com.ruoyi.manager.domain.MgServiceType;
import com.ruoyi.manager.service.IMgServiceManagerService;
import com.ruoyi.manager.service.IMgServiceTypeService;
import com.ruoyi.oss.config.OssConfig;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务管理Controller
 * 
 * @author ruoyi
 * @date 2023-12-13
 */
@RestController
@RequestMapping("/manager")
public class MgServiceManagerController extends BaseController
{
    @Autowired
    private IMgServiceManagerService mgServiceManagerService;
    
    @Autowired
    private IMgServiceTypeService serviceTypeService;
    
    private OssConfig config;

    /**
     * 查询服务管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MgServiceManager mgServiceManager)
    {
    	String picPrefix = config.getBucketName().concat(".").concat(config.getEndpoint());
        startPage();
        List<MgServiceManager> list = mgServiceManagerService.selectMgServiceManagerList(mgServiceManager, picPrefix);
        return getDataTable(list);
    }

    /**
     * 导出服务管理列表
     */
    @PreAuthorize("@ss.hasPermi('manager:manager:export')")
    @Log(title = "服务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MgServiceManager mgServiceManager)
    {
    	String picPrefix = config.getBucketName().concat(".").concat(config.getEndpoint());

    	
        List<MgServiceManager> list = mgServiceManagerService.selectMgServiceManagerList(mgServiceManager, picPrefix);
        ExcelUtil<MgServiceManager> util = new ExcelUtil<MgServiceManager>(MgServiceManager.class);
        util.exportExcel(response, list, "服务管理数据");
    }

    /**
     * 获取服务管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:manager:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
    	String picPrefix = "https://".concat(config.getBucketName().concat(".").concat(config.getEndpoint()));
    	
        MgServiceManager selectMgServiceManagerById = mgServiceManagerService.selectMgServiceManagerById(id);
        
        selectMgServiceManagerById.setPic(picPrefix.concat(selectMgServiceManagerById.getPic()));
        
		return success(selectMgServiceManagerById);
    }

    /**
     * 新增服务管理
     */
//    @PreAuthorize("@ss.hasPermi('manager:manager:add')")
//    @Log(title = "服务管理", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody MgServiceManager mgServiceManager)
//    {
//    	MgServiceType mgServiceType = serviceTypeService.geterviceType(mgServiceManager.getServiceType());
//    	
//    	if (null == mgServiceType) {
//    		return AjaxResult.error("找不到服务类型");
//    	}
//    	
//    	String picPrefix = config.getBucketName().concat(".").concat(config.getEndpoint());
//    	
//    	
//    	mgServiceManager.setTypeName(mgServiceType.getTypeName());
//    	
//        return toAjax(mgServiceManagerService.insertMgServiceManager(mgServiceManager, picPrefix));
//    }

    /**
     * 修改服务管理
     */
    @PreAuthorize("@ss.hasPermi('manager:manager:edit')")
    @Log(title = "服务管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MgServiceManager mgServiceManager)
    {
    	MgServiceType mgServiceType = serviceTypeService.geterviceType(mgServiceManager.getServiceType());
    	
    	if (null == mgServiceType) {
    		return AjaxResult.error("找不到服务类型");
    	}
    	
    	String picPrefix = config.getURL();
    	
    	
    	mgServiceManager.setTypeName(mgServiceType.getTypeName());
    	
        return toAjax(mgServiceManagerService.updateMgServiceManager(mgServiceManager, picPrefix));
    }

    /**
     * 删除服务管理
     */
    @PreAuthorize("@ss.hasPermi('manager:manager:remove')")
    @Log(title = "服务管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mgServiceManagerService.deleteMgServiceManagerByIds(ids));
    }

    @Resource
	public void setConfig(OssConfig config) {
		this.config = config;
	}
}
